/************************************************************************************/
/*  Program name CAPAPIMP_INV_SELECT_CRT.sql                                        */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_INV_SELECT_CRT';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_INV_SELECT_CRT;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_INV_SELECTION_CRITERIA_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_INV_SELECT_CRT_MAP VALUES
			(
			C1.CHECKRUN_NAME,
			C1.ORG_ID,
			C1.CHECKRUN_ID,
			C1.CHECKRUN_NAME,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),
			lv_Next_Seq_ID
			);

			INSERT INTO AP_INV_SELECTION_CRITERIA_ALL
			VALUES
			(
			C1.CHECKRUN_NAME,
			C1.CHECK_DATE,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),				--LAST_UPDATED_BY,
			C1.BANK_ACCOUNT_NAME,
			C1.PERIOD_NAME,
			C1.PAY_THRU_DATE,
			C1.VENDOR_PAY_GROUP,
			C1.HI_PAYMENT_PRIORITY,
			C1.LOW_PAYMENT_PRIORITY,
			C1.MAX_PAYMENT_AMOUNT,
			C1.MIN_CHECK_AMOUNT,
			C1.MAX_OUTLAY,
			C1.PAY_ONLY_WHEN_DUE_FLAG,
			C1.STATUS,
			CAPBRIMP_MAPPING.GetCheckStockIDMapping(C1.CHECK_STOCK_ID),		--CHECK_STOCK_ID
			C1.CURRENCY_CODE,
			C1.EXCHANGE_RATE,
			C1.EXCHANGE_RATE_TYPE,
			C1.EXCHANGE_DATE,
			C1.DOCUMENT_ORDER_LOOKUP_CODE,
			C1.AUDIT_REQUIRED_FLAG,
			C1.INTERVAL,
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),					--CREATED_BY,
			C1.VOLUME_SERIAL_NUMBER,
			C1.ATTRIBUTE_CATEGORY,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.USSGL_TRANSACTION_CODE,
			C1.USSGL_TRX_CODE_CONTEXT,
			C1.ZERO_AMOUNTS_ALLOWED,
			C1.START_PRINT_DOCUMENT,
			C1.END_PRINT_DOCUMENT,
			C1.FIRST_VOUCHER_NUMBER,
			C1.FIRST_AVAILABLE_DOCUMENT,
			C1.PAYMENT_METHOD_LOOKUP_CODE,
			C1.ZERO_INVOICES_ALLOWED,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),							--ORG_ID,
			lv_Next_Seq_ID,													--CHECKRUN_ID,
			C1.BATCH_IDENTIFIER,
			CAPBRIMP_MAPPING.GetBankACMapping(C1.BANK_ACCOUNT_ID),			--BANK_ACCOUNT_ID,
			C1.TEMPLATE_FLAG,
			C1.TRANSFER_PRIORITY,
			C1.BATCH_RUN_NAME,
			C1.BATCH_SET_ID,
			C1.BATCH_SET_LINE_ID,
			C1.FUTURE_DATED_PAYMENT_FLAG,
			C1.ANTICIPATED_VALUE_DATE,
			CAPBRIMP_MAPPING.GetAPBatchIDMapping(C1.INVOICE_BATCH_ID),		--INVOICE_BATCH_ID,
			CAPBRIMP_COMMON.GetVendorID_Name(C1.VENDOR_NAME)				--VENDOR_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
